package cn.controller;

import cn.entity.Result;
import cn.entity.SysResource;
import cn.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/resource")
public class SysResourceController {
    @Autowired
    SysResourceService sysResourceService;

    @RequestMapping("/resource")
    public Result listResource(){
        SysResource sysResource = new SysResource();
        sysResource.setDelFlag("0");
        sysResource.setType("0");
        return new Result(true,"查询成功",sysResourceService.select(sysResource));
    }

    @RequestMapping("/selectResourceByRid")
    public Result selectResourceByRid(Long id){
        return new Result(true,"查询成功",sysResourceService.selectResourceByRid(id));
    }

    @RequestMapping("/initMenu/{id}")
    public Result initMenu(@PathVariable("id") Long id){
        List<SysResource> resources = sysResourceService.selectResourceByRid(id);
        for (SysResource resource : resources) {
            String url = resource.getUrl();
            if (url!=null) {
                if (!url.equals("")) {
                    resource.setUrl(url.replace("manager/", ""));
                }
            }
        }
        return new Result(true,"查询成功",resources);
    }
}
